using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;

namespace Scorm_Content_Editor
{
    public partial class MainForm : Form
    {
        private CoursePanel PanelCourse = new CoursePanel();
        TreeNode CourseNode = new TreeNode();
        TreeNode ModuleNode = new TreeNode();
        string filename;
        string windowname;

        public MainForm()
        {
            InitializeComponent();
            PanelCourse.Owner = this;
            PanelCourse.Text = "Course Explorer";
            PanelCourse.TabText = "Course Explorer";
            PanelCourse.Show(dockPanel);
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void CourseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.create_course();
        }

        void treeView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                //lokasi dimana context menu strip akan ditampilkan
                PanelCourse.treeView1.SelectedNode = PanelCourse.treeView1.GetNodeAt(e.X, e.Y);

                if (PanelCourse.treeView1.SelectedNode.Level == 0)
                {
                    contextMenuStripPanelCourse0.Show(PanelCourse.treeView1, e.Location);
                }
                if (PanelCourse.treeView1.SelectedNode.Level == 1)
                {
                    contextMenuStripPanelCourse.Show(PanelCourse.treeView1, e.Location);
                }
                if (PanelCourse.treeView1.SelectedNode.Level == 2)
                {
                    contextMenuStripPanelCourse2.Show(PanelCourse.treeView1, e.Location);
                }
            }
           
        }


        private void frmMain_Load(object sender, EventArgs e)
        {
            
        }

        private void CourseToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            PanelCourse.Show(dockPanel);
        }

        private void AboutSCORMConeditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 frmAbout = new AboutBox1();
            frmAbout.ShowDialog();
        }

        private void ToolStripButton1_Click(object sender, EventArgs e)
        {
            CourseToolStripMenuItem_Click(null, null);
        }

        private void ToolStripButton2_Click(object sender, EventArgs e)
        {
            this.open_course();
        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripButton2_Click(null, null);
        }

        private void TextBoxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextEditor textEdit = new TextEditor();

            if (textEdit.ShowDialog() == DialogResult.OK)
            {
                Label txtLabel = new Label();
                txtLabel.Text = textEdit.richTextBox1.Text;
                txtLabel.BackColor = Color.White;
                txtLabel.Location = new Point(34, 54);
                txtLabel.AutoSize = true;
                txtLabel.Name = "text";
                this.pemaksaan();
                ActiveMdiChild.ActiveControl.Controls.Add(txtLabel);

                //add an event to the label
                txtLabel.MouseDown += new MouseEventHandler(txtLabel_MouseDown);
                txtLabel.MouseMove += new MouseEventHandler(ctrl_MouseMove);
                txtLabel.MouseUp += new MouseEventHandler(ctrl_MouseUp);

                ////bring to front


            }
        }

        private int clickOffsetX, clickOffsetY;
        private Label lblToFocus;
        private PictureBox pictureToFocus;
        private bool isDragging = false;
        private bool isResizing = false;

        private void txtLabel_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Retrieve a reference to the active label.           
            Label currentLabel;
            currentLabel = (Label)sender;


            // Required for the focus rectangle.
            lblToFocus = currentLabel;


            // Invalidate to show the focus rectangle.
            Invalidate();
            if (e.Button == MouseButtons.Right)
            {
                // Show the context menu.
                //currentLabel.ContextMenuStrip.Show(currentLabel, new Point(e.X, e.Y));

                contextMenuStripCtrlTxtLabel.Show(currentLabel, new Point(e.X, e.Y));
                //currentLabel.Text = "babi";
            }

            if (e.Button == MouseButtons.Left)
            {
                clickOffsetX = e.X;
                clickOffsetY = e.Y;

                if (currentLabel.Cursor == Cursors.SizeNWSE ||
                    currentLabel.Cursor == Cursors.SizeNS ||
                    currentLabel.Cursor == Cursors.SizeWE)
                {
                    // The mouse pointer is at one of the sides,
                    // so resizing mode is appropriate.

                    isResizing = true;
                }
                else
                {
                    // The mouse is somewhere else, so dragging mode is
                    // appropriate.
                    isDragging = true;
                }
            }
        }

        private void ctrl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Retrieve a reference to the active shape.
            Control currentCtrl;
            currentCtrl = (Control)sender;


            if (isDragging)
            {
                // Move the control.
                currentCtrl.Left = e.X + currentCtrl.Left - clickOffsetX;
                currentCtrl.Top = e.Y + currentCtrl.Top - clickOffsetY;
            }
            else if (isResizing)
            {
                // Resize the control, according to the resize mode.
                if (currentCtrl.Cursor == Cursors.SizeNWSE)
                {
                    currentCtrl.Width = e.X;
                    currentCtrl.Height = e.Y;
                }
                else if (currentCtrl.Cursor == Cursors.SizeNS)
                {
                    currentCtrl.Height = e.Y;
                }
                else if (currentCtrl.Cursor == Cursors.SizeWE)
                {
                    currentCtrl.Width = e.X;
                }
            }
            else
            {
                // Change the cursor if the mouse pointer is on one of the edges
                // of the control.
                if (((e.X + 5) > currentCtrl.Width) &&
                   ((e.Y + 5) > currentCtrl.Height))
                {
                    currentCtrl.Cursor = Cursors.SizeNWSE;
                }
                else if ((e.X + 5) > currentCtrl.Width)
                {
                    currentCtrl.Cursor = Cursors.SizeWE;
                }
                else if ((e.Y + 5) > currentCtrl.Height)
                {
                    currentCtrl.Cursor = Cursors.SizeNS;
                }
                else
                {
                    // This misleadingly named cursor is the four-way
                    // mouse pointer often used for moving objects.
                    currentCtrl.Cursor = Cursors.SizeAll;
                }
            }
        }

        private void ctrl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isDragging = false;
            isResizing = false;

            // Invalidate to show the focus rectangle.
            Invalidate();

        }

        private void editToolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            Control currentLabel = contextMenuStripCtrlTxtLabel.SourceControl;
            TextEditor textEdit = new TextEditor();
            textEdit.richTextBox1.Text = currentLabel.Text;

            if (textEdit.ShowDialog() == DialogResult.OK)
            {
                currentLabel.Text = textEdit.richTextBox1.Text;
            }
        }

        private void reToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Control currentLabel = contextMenuStripCtrlTxtLabel.SourceControl;
            ActiveMdiChild.ActiveControl.Controls.Remove(currentLabel);
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int x = 0; x < this.MdiChildren.Length; x++)
            {
                for (int y = 0; y < PanelCourse.treeView1.SelectedNode.Nodes.Count; y++)
                {
                    SlideDocument tempDocument = (SlideDocument)this.MdiChildren[x];
                    if (tempDocument.TabText == PanelCourse.treeView1.SelectedNode.Text + " -- " + PanelCourse.treeView1.SelectedNode.Nodes[y].Text)
                    {
                        tempDocument.HideOnClose = false;
                        tempDocument.Close();
                    }
                }
            }
            
            int indexnode = PanelCourse.treeView1.SelectedNode.Index;
            int moduleDirectory = indexnode + 1;
            FileInfo fi = new FileInfo(filename);
            DirectoryInfo di = fi.Directory;
            //MessageBox.Show(di.FullName.ToString() + "\\" + moduleDirectory.ToString());
            Directory.Delete(di.FullName.ToString() + "\\" + moduleDirectory.ToString(), true);
            PanelCourse.treeView1.Nodes.Remove(PanelCourse.treeView1.SelectedNode);
            this.Save();
        }

        private void create_course()
        {
            NewCourse newCourse = new NewCourse();
            newCourse.FormBorderStyle = FormBorderStyle.FixedDialog;
            newCourse.MaximizeBox = false;
            newCourse.MinimizeBox = false;
            newCourse.StartPosition = FormStartPosition.CenterScreen;
            newCourse.Text = "New Course";

            if (newCourse.ShowDialog() == DialogResult.OK)
            {
                #region create xml sce file
                //Create the directory
                Directory.CreateDirectory(newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text);

                //pemasukkan ke global variabel
                filename = newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\" + newCourse.txtName.Text + ".rse";
                //create a strem writer
                XmlTextWriter xtw = new XmlTextWriter(newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\" + newCourse.txtName.Text + ".rse", Encoding.UTF8);
                xtw.Formatting = Formatting.Indented;

                //start document
                xtw.WriteStartDocument();
                xtw.WriteComment("SCE Course");

                //start element course name (root)
                xtw.WriteStartElement("SCE");

                xtw.WriteElementString("aicc_code", "");
                xtw.WriteElementString("aicc_name", newCourse.txtName.Text);
                xtw.WriteElementString("aicc_description", "");

                //start element modules (level_1)
                xtw.WriteStartElement("modules");

                //start element module (level_2)
                xtw.WriteStartElement("module");
                xtw.WriteElementString("name", "1");
                xtw.WriteElementString("folder_id", "0");
                xtw.WriteElementString("file", "1\\1.xml");
                xtw.WriteElementString("aicc_code", "");
                xtw.WriteElementString("aicc_name", newCourse.txtModuleName.Text);
                xtw.WriteElementString("aicc_description", "");
                xtw.WriteElementString("aicc_maxmark", "");
                xtw.WriteElementString("aicc_reqmark", "");
                //end element module (level_2)
                xtw.WriteEndElement();



                //end element modules (level_1)
                xtw.WriteEndElement();


                //windows
                xtw.WriteStartElement("windows");
                //window
                xtw.WriteStartElement("window");
                xtw.WriteElementString("module", "1");
                xtw.WriteElementString("zoom", "100");
                xtw.WriteElementString("active", "true");
                xtw.WriteElementString("slide", "SLIDE_1");
                //window
                xtw.WriteEndElement();
                //windows
                xtw.WriteEndElement();





                //end element course name (root)
                xtw.WriteEndElement();




                //end document
                xtw.WriteEndDocument();
                xtw.Close();
                #endregion

                #region create module directory & file
                //setiap pencipataan module dimulai dengan direktori 1 kemudian 2 dst... (untuk sementara masih manual)
                //seharusnya jika file->new Module dipanggil maka akan menambahkan satu direktori secara otomatis.
                //begitupun dengan file xml nya.
                Directory.CreateDirectory(newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\" + "1");

                #region create xml module file
                XmlTextWriter xtwm = new XmlTextWriter(newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\" + "1" + "\\" + "1.xml", Encoding.UTF8);
                xtwm.Formatting = Formatting.Indented;
                //start document
                xtwm.WriteStartDocument();
                xtwm.WriteComment("Module file, created by SCE");

                #region root element module
                xtwm.WriteStartElement("module");
                xtwm.WriteAttributeString("format", "0.1");
                xtwm.WriteAttributeString("rev", "1");
                xtwm.WriteAttributeString("slideWidth", "750");
                xtwm.WriteAttributeString("slideHeight", "550");
                xtwm.WriteAttributeString("template", "empty");
                xtwm.WriteAttributeString("file", newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\" + "1" + "\\" + "1.xml");
                #region level 1 element font
                xtwm.WriteStartElement("font");
                xtwm.WriteAttributeString("face", "Arial");
                xtwm.WriteAttributeString("style", "1");
                xtwm.WriteAttributeString("size", "12");
                xtwm.WriteAttributeString("color", "#000000");
                xtwm.WriteEndElement();
                #endregion
                #region level 1 element newids
                xtwm.WriteStartElement("newids");
                xtwm.WriteAttributeString("slide", "SLIDE_");
                xtwm.WriteAttributeString("subslide", "FRAME_");
                xtwm.WriteAttributeString("iebox", "TXT_");
                xtwm.WriteAttributeString("image", "IMG_");
                xtwm.WriteAttributeString("object", "OBJ_");
                xtwm.WriteAttributeString("group", "GROUP_");
                xtwm.WriteEndElement();
                #endregion
                #region level 1 element import
                xtwm.WriteStartElement("import");
                xtwm.WriteAttributeString("masterid", "");
                xtwm.WriteAttributeString("left", "0");
                xtwm.WriteAttributeString("top", "0");
                xtwm.WriteAttributeString("width", "0");
                xtwm.WriteAttributeString("height", "0");
                xtwm.WriteAttributeString("commentsLeft", "0");
                xtwm.WriteAttributeString("commentsTop", "0");
                xtwm.WriteAttributeString("commentsWidth", "0");
                xtwm.WriteAttributeString("commentsHeight", "0");
                xtwm.WriteAttributeString("importCommentsLeft", "no");
                xtwm.WriteAttributeString("commentscolor", "#000000");
                xtwm.WriteAttributeString("fiximages", "yes");

                xtwm.WriteEndElement();
                #endregion
                #region level 1 element runtime
                xtwm.WriteStartElement("runtime");
                xtwm.WriteAttributeString("showsplash", "yes");
                xtwm.WriteAttributeString("seqslide", "no");
                xtwm.WriteAttributeString("preloadimages", "yes");
                xtwm.WriteAttributeString("normalizes", "no");
                xtwm.WriteEndElement();
                #endregion
                #region level 1 element nextdentity
                xtwm.WriteStartElement("nextidentity");
                xtwm.WriteAttributeString("slide", "17");
                xtwm.WriteAttributeString("subslide", "0");
                xtwm.WriteAttributeString("object", "0");
                xtwm.WriteAttributeString("group", "0");
                xtwm.WriteAttributeString("mousepointer", "0");
                xtwm.WriteAttributeString("actionbox", "0");
                xtwm.WriteEndElement();

                #endregion
                #region level 1 element slides
                xtwm.WriteStartElement("slides");
                xtwm.WriteStartElement("slide");
                xtwm.WriteAttributeString("guid", "");
                xtwm.WriteAttributeString("id", "");
                xtwm.WriteAttributeString("sid", "");
                xtwm.WriteAttributeString("name", newCourse.txtModuleName.Text);
                xtwm.WriteEndElement();
                xtwm.WriteEndElement();
                #endregion
                #region level 1 element runtimechecks
                xtwm.WriteStartElement("runtimechecks");
                #region level 2 element runtimecheck
                xtwm.WriteStartElement("runtimecheck");
                xtwm.WriteAttributeString("id", "");
                xtwm.WriteAttributeString("check", "");
                xtwm.WriteEndElement();
                #endregion
                #region level 2 element runtimecheck
                xtwm.WriteStartElement("runtimecheck");
                xtwm.WriteAttributeString("id", "");
                xtwm.WriteAttributeString("check", "");
                xtwm.WriteEndElement();
                #endregion
                #region level 2 element runtimecheck
                xtwm.WriteStartElement("runtimecheck");
                xtwm.WriteAttributeString("id", "");
                xtwm.WriteAttributeString("check", "");
                xtwm.WriteEndElement();
                #endregion
                #region level 2 element runtimecheck
                xtwm.WriteStartElement("runtimecheck");
                xtwm.WriteAttributeString("id", "");
                xtwm.WriteAttributeString("check", "");
                xtwm.WriteEndElement();
                #endregion
                xtwm.WriteEndElement();
                #endregion

                xtwm.WriteEndElement();
                #endregion

                xtwm.WriteEndDocument();
                xtwm.Close();
                #endregion


                #endregion

                SlideDocument moduleWindow = new SlideDocument();
                moduleWindow.Text = newCourse.txtModuleName.Text + " -- SLIDE_1";
                moduleWindow.TabText = newCourse.txtModuleName.Text + " -- SLIDE_1";
                moduleWindow.MdiParent = this;
                moduleWindow.Show(dockPanel);
                
                CourseNode = PanelCourse.treeView1.Nodes.Add(newCourse.txtName.Text);
                ModuleNode = CourseNode.Nodes.Add(newCourse.txtModuleName.Text);
                ModuleNode.Nodes.Add("SLIDE_1");
                
                //add event to the panel course
                PanelCourse.treeView1.MouseUp += new MouseEventHandler(treeView1_MouseUp);
                PanelCourse.treeView1.ExpandAll();

                //enabling the menu

                this.enable_menu();
                this.Text = "Scorm Content Editor -- " + newCourse.txtName.Text;

                //copy all hta and friends
                Directory.CreateDirectory(newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\courseimages");

                File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\legacy.js", newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\courseimages\\legacy.js");
                File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\lms.js", newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\courseimages\\lms.js");
                File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\lab.js", newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\courseimages\\lab.js");

                File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\start.hta", newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\1\\start.hta");
                File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\start.html", newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\1\\start.html");
                File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\module.js", newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\1\\module.js");
                File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\course.css", newCourse.txtLocation.Text + "\\" + newCourse.txtFolderName.Text + "\\1\\course.css");
            }
        }

        private void enable_menu()
        {
            SaveToolStripMenuItem.Enabled = true;
            PublishToolStripMenuItem.Enabled = true;
            TextBoxToolStripMenuItem.Enabled = true;
            FromFileToolStripMenuItem.Enabled = true;
            startButtonToolStripMenuItem.Enabled = true;
            nextButtonToolStripMenuItem.Enabled = true;
            previousButtonToolStripMenuItem.Enabled = true;
            newModuleToolStripMenuItem.Enabled = true;
        }

        private void open_course()
        {
            OpenFileDialog openFile = new OpenFileDialog();

            //openFile.InitialDirectory = Application.ExecutablePath;
            openFile.Filter = "reza scorm editor files (*.rse)|*.rse";
            openFile.FilterIndex = 1;
            openFile.RestoreDirectory = true;

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                String fullName = openFile.FileName;
                
                //masukkan ke global variabel filename
                filename = openFile.FileName;

                Form moduleForm = new Form();
                Form slideform = new Form();
                
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(fullName);
                XmlNode oNode = xmldoc.DocumentElement;

                XmlNodeList oNodeList = oNode.SelectNodes("/SCE");
                for (int x = 0; x < oNodeList.Count; x++)
                {
                    for (int i = 0; i < oNodeList.Item(x).ChildNodes.Count; i++)
                    {
                        if (oNodeList.Item(x).ChildNodes[i].Name == "aicc_name")
                        {
                            CourseNode = PanelCourse.treeView1.Nodes.Add(oNodeList.Item(x).ChildNodes[i].InnerText);
                        }
                    }
                }

                XmlNodeList oNodeListModule = oNode.SelectNodes("/SCE/modules/module");
                XmlNodeList oNodeListSlide = oNode.SelectNodes("/SCE/windows/window");
                for (int x = 0; x < oNodeListModule.Count; x++)
                {
                    for (int i = 0; i < oNodeListModule.Item(x).ChildNodes.Count; i++)
                    {
                        if (oNodeListModule.Item(x).ChildNodes[i].Name == "aicc_name")
                        {
                            ModuleNode = CourseNode.Nodes.Add(oNodeListModule.Item(x).ChildNodes[i].InnerText);
                            //MessageBox.Show(ModuleNode.Text);

                            for (int y = 0; y < oNodeListSlide.Count; y++)
                            {
                                for (int j = 0; j < oNodeListSlide.Item(x).ChildNodes.Count; j++)
                                {
                                    if (oNodeListSlide.Item(y).ChildNodes[j].Name == "module")
                                    {
                                        if (oNodeListSlide.Item(y).ChildNodes[j].InnerText == oNodeListModule.Item(x).ChildNodes[0].InnerText)
                                        {
                                            ModuleNode.Nodes.Add(oNodeListSlide.Item(y).ChildNodes[3].InnerText);
                                            SlideDocument slide = new SlideDocument();
                                            slide.TabText = ModuleNode.Text + " -- " + oNodeListSlide.Item(y).ChildNodes[3].InnerText;
                                            slide.Show(dockPanel);

                                            //buka file dalam directory ; 1\1.xml
                                            FileInfo fi = new FileInfo(fullName);
                                            DirectoryInfo di = fi.Directory;
                                            string modfile = di + "\\" + oNodeListSlide.Item(y).ChildNodes[j].InnerText + "\\" + oNodeListSlide.Item(y).ChildNodes[j].InnerText + ".xml";
                                            XmlDocument modulesDoc = new XmlDocument();
                                            modulesDoc.Load(modfile);
                                            XmlNode modNode = modulesDoc.DocumentElement;
                                            XmlNodeList oModNode = modulesDoc.SelectNodes("/module/slides/slide/Objects/object");
                                            //MessageBox.Show(oModNode.Count.ToString());
                                            //MessageBox.Show(oModNode.Item(y).ChildNodes.Count.ToString());
                                           for (int z = 0; z < oModNode.Count; z++)
                                            {
                                            
                                                    //MessageBox.Show(oModNode.Item(y).ChildNodes.Item(xx).Name);
                                                   //MessageBox.Show(oModNode.Item(z).ChildNodes.Item(0).InnerText);
                                               // MessageBox.Show(oModNode.Count.ToString());

                                                if (oModNode.Item(z).ChildNodes.Item(0).InnerText == oNodeListSlide.Item(y).ChildNodes[3].InnerText)
                                                {
                                                    if (oModNode.Item(z).ChildNodes.Item(1).InnerText == "text")
                                                    {
                                                        Label baru = new Label();
                                                        int posTextX = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(3).InnerText);
                                                        int posTextY = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(4).InnerText);
                                                        baru.Text = oModNode.Item(z).ChildNodes.Item(2).InnerText;
                                                        baru.AutoSize = true;
                                                        baru.Location = new Point(posTextX, posTextY);
                                                        baru.Name = "text";
                                                        ActiveMdiChild.ActiveControl.Controls.Add(baru);
                                                        //add an event to the label
                                                        baru.MouseDown += new MouseEventHandler(txtLabel_MouseDown);
                                                        baru.MouseMove += new MouseEventHandler(ctrl_MouseMove);
                                                        baru.MouseUp += new MouseEventHandler(ctrl_MouseUp);

                                                    }
                                                    if (oModNode.Item(z).ChildNodes.Item(1).InnerText == "picture")
                                                    {
                                                        Bitmap gambarnya = new Bitmap(oModNode.Item(z).ChildNodes.Item(2).InnerText);
                                                        int posPicX = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(3).InnerText);
                                                        int posPicY = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(4).InnerText);
                                                        PictureBox ctrlPictureBox = new PictureBox();
                                                        ctrlPictureBox.Location = new Point(posPicX, posPicY);
                                                        ctrlPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
                                                        ctrlPictureBox.Name = "picture";
                                                        ctrlPictureBox.Tag = oModNode.Item(z).ChildNodes.Item(2).InnerText;
                                                        ctrlPictureBox.Image = (Image)gambarnya;
                                                        ActiveMdiChild.ActiveControl.Controls.Add(ctrlPictureBox);

                                                        ctrlPictureBox.MouseDown += new MouseEventHandler(ctrlPictureBox_MouseDown);
                                                        ctrlPictureBox.MouseUp += new MouseEventHandler(ctrlPictureBox_MouseUp);
                                                        ctrlPictureBox.MouseMove += new MouseEventHandler(ctrlPictureBox_MouseMove);
                                                    }
                                                    if (oModNode.Item(z).ChildNodes.Item(1).InnerText == "StartButton")
                                                    {
                                                        Bitmap gambarnya = new Bitmap(oModNode.Item(z).ChildNodes.Item(2).InnerText);
                                                        int posPicX = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(3).InnerText);
                                                        int posPicY = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(4).InnerText);
                                                        PictureBox ctrlPictureBox = new PictureBox();
                                                        ctrlPictureBox.Location = new Point(posPicX, posPicY);
                                                        ctrlPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
                                                        ctrlPictureBox.Name = "StartButton";
                                                        ctrlPictureBox.Tag = oModNode.Item(z).ChildNodes.Item(2).InnerText;
                                                        ctrlPictureBox.Image = (Image)gambarnya;
                                                        ActiveMdiChild.ActiveControl.Controls.Add(ctrlPictureBox);

                                                        ctrlPictureBox.MouseDown += new MouseEventHandler(ctrlPictureBox_MouseDown);
                                                        ctrlPictureBox.MouseUp += new MouseEventHandler(ctrlPictureBox_MouseUp);
                                                        ctrlPictureBox.MouseMove += new MouseEventHandler(ctrlPictureBox_MouseMove);
                                                    }
                                                    if (oModNode.Item(z).ChildNodes.Item(1).InnerText == "NextButton")
                                                    {
                                                        Bitmap gambarnya = new Bitmap(oModNode.Item(z).ChildNodes.Item(2).InnerText);
                                                        int posPicX = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(3).InnerText);
                                                        int posPicY = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(4).InnerText);
                                                        PictureBox ctrlPictureBox = new PictureBox();
                                                        ctrlPictureBox.Location = new Point(posPicX, posPicY);
                                                        ctrlPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
                                                        ctrlPictureBox.Name = "NextButton";
                                                        ctrlPictureBox.Tag = oModNode.Item(z).ChildNodes.Item(2).InnerText;
                                                        ctrlPictureBox.Image = (Image)gambarnya;
                                                        ActiveMdiChild.ActiveControl.Controls.Add(ctrlPictureBox);

                                                        ctrlPictureBox.MouseDown += new MouseEventHandler(ctrlPictureBox_MouseDown);
                                                        ctrlPictureBox.MouseUp += new MouseEventHandler(ctrlPictureBox_MouseUp);
                                                        ctrlPictureBox.MouseMove += new MouseEventHandler(ctrlPictureBox_MouseMove);
                                                    }
                                                    if (oModNode.Item(z).ChildNodes.Item(1).InnerText == "PrevButton")
                                                    {
                                                        Bitmap gambarnya = new Bitmap(oModNode.Item(z).ChildNodes.Item(2).InnerText);
                                                        int posPicX = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(3).InnerText);
                                                        int posPicY = Convert.ToInt32(oModNode.Item(z).ChildNodes.Item(4).InnerText);
                                                        PictureBox ctrlPictureBox = new PictureBox();
                                                        ctrlPictureBox.Location = new Point(posPicX, posPicY);
                                                        ctrlPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
                                                        ctrlPictureBox.Name = "PrevButton";
                                                        ctrlPictureBox.Tag = oModNode.Item(z).ChildNodes.Item(2).InnerText;
                                                        ctrlPictureBox.Image = (Image)gambarnya;
                                                        ActiveMdiChild.ActiveControl.Controls.Add(ctrlPictureBox);

                                                        ctrlPictureBox.MouseDown += new MouseEventHandler(ctrlPictureBox_MouseDown);
                                                        ctrlPictureBox.MouseUp += new MouseEventHandler(ctrlPictureBox_MouseUp);
                                                        ctrlPictureBox.MouseMove += new MouseEventHandler(ctrlPictureBox_MouseMove);
                                                    }
                                                }
                                             
                                                

                                            //Label baru = new Label();
                                            //baru.Text = oModNode.Item(y).InnerText;
                                            //ActiveMdiChild.ActiveControl.Controls.Add(baru);
                                                
                                                
                                           }

                                            //batasnya
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                
              


                //add event to the panel course
                PanelCourse.treeView1.MouseUp += new MouseEventHandler(treeView1_MouseUp);
                PanelCourse.treeView1.ExpandAll();

                this.enable_menu();
           

            }
        }

        private void create_module(string name)
        {
           
        }

        private void create_slide(string name)
        {
        }

        private void delete_module(string name)
        {
            MessageBox.Show(name);
        }

        private void delete_slide(string name)
        {
        }

        private void addSlideToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int NodeSaatIni = PanelCourse.treeView1.SelectedNode.Nodes.Count + 1;
            SlideDocument slideBaru = new SlideDocument();
            slideBaru.TabText = PanelCourse.treeView1.SelectedNode.Text + " -- SLIDE_" + NodeSaatIni.ToString();
            slideBaru.Text = PanelCourse.treeView1.SelectedNode.Text + " -- SLIDE_" + NodeSaatIni.ToString();
            PanelCourse.treeView1.SelectedNode.Nodes.Add("SLIDE_" + NodeSaatIni.ToString());
            slideBaru.Show(dockPanel);
            PanelCourse.treeView1.ExpandAll();
            this.Save();
        }

        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            for (int x = 0; x < this.MdiChildren.Length; x++)
            {
                SlideDocument tempDocument = (SlideDocument)this.MdiChildren[x];
               
                if (tempDocument.TabText == PanelCourse.treeView1.SelectedNode.Text + " -- SLIDE_1")
                {
                    tempDocument.Show(dockPanel);
                }
            }
            
        }

        private void viewToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            for (int x = 0; x < this.MdiChildren.Length; x++)
            {
                SlideDocument tempDocument = (SlideDocument)this.MdiChildren[x];

                if (tempDocument.TabText == PanelCourse.treeView1.SelectedNode.Parent.Text + " -- " + PanelCourse.treeView1.SelectedNode.Text)
                {
                    tempDocument.Show(dockPanel);
                }
            }
        }

        private void removeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (PanelCourse.treeView1.SelectedNode.NextNode == null)
            {
                for (int x = 0; x < this.MdiChildren.Length; x++)
                {
                    SlideDocument tempDocument = (SlideDocument)this.MdiChildren[x];

                    if (tempDocument.TabText == PanelCourse.treeView1.SelectedNode.Parent.Text + " -- " + PanelCourse.treeView1.SelectedNode.Text)
                    {
                        tempDocument.HideOnClose = false;
                        tempDocument.Close();
                        PanelCourse.treeView1.Nodes.Remove(PanelCourse.treeView1.SelectedNode);
                    }
                }
            }
            else
            {


                for (int x = 0; x < this.MdiChildren.Length; x++)
                {
                    SlideDocument tempDocument = (SlideDocument)this.MdiChildren[x];

                    if (tempDocument.TabText == PanelCourse.treeView1.SelectedNode.Parent.Text + " -- " + PanelCourse.treeView1.SelectedNode.Text)
                    {
                        tempDocument.HideOnClose = false;
                        tempDocument.Close();
                        PanelCourse.treeView1.Nodes.Remove(PanelCourse.treeView1.SelectedNode);
                    }
                }


                for (int y = 0; y < PanelCourse.treeView1.SelectedNode.Parent.Nodes.Count; y++)
                {
                    int b = y + 1;
                    //MessageBox.Show(PanelCourse.treeView1.SelectedNode.Parent.Nodes.Count.ToString());
                    for (int x = 0; x < this.MdiChildren.Length; x++)
                    {
                        SlideDocument tempDocument = (SlideDocument)this.MdiChildren[x];
                        if (tempDocument.TabText == PanelCourse.treeView1.SelectedNode.Parent.Text + " -- " + PanelCourse.treeView1.SelectedNode.Parent.Nodes[y].Text)
                        {
                            int a = y + 1;
                            tempDocument.TabText = PanelCourse.treeView1.SelectedNode.Parent.Text + " -- SLIDE_" + a.ToString();
                        }
                    }
                }
                int modnode = PanelCourse.treeView1.SelectedNode.Parent.Nodes.Count;
                int indexNode = PanelCourse.treeView1.SelectedNode.Parent.Index;
                string modname = PanelCourse.treeView1.SelectedNode.Parent.Text;

                PanelCourse.treeView1.SelectedNode.Parent.Remove();
                ModuleNode = CourseNode.Nodes.Insert(indexNode, modname);

                for (int x = 0; x < modnode; x++)
                {
                    int b = x + 1;
                    ModuleNode.Nodes.Add("SLIDE_" + b);
                }

                PanelCourse.treeView1.ExpandAll();
            }
            
        }

        private void addModuleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewModule newModule = new NewModule();
            newModule.FormBorderStyle = FormBorderStyle.FixedDialog;
            newModule.StartPosition = FormStartPosition.CenterScreen;
            if (newModule.ShowDialog() == DialogResult.OK)
            {
                ModuleNode = CourseNode.Nodes.Add(newModule.txtModuleName.Text);
            }
            PanelCourse.treeView1.ExpandAll();
            this.Save();
        }

        private void FromFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog browseImage = new OpenFileDialog();
            browseImage.Filter = "JPEG(*.JPG;*.JPEG)|*.JPG;*.JPEG|GIF(*.gif)|*.gif|BMP(*.bmp)|*.bmp";
            browseImage.FilterIndex = 2;
            //browseImage.InitialDirectory = 
            browseImage.RestoreDirectory = true;
            if (browseImage.ShowDialog() == DialogResult.OK)
            {
                Bitmap gambarnya = new Bitmap(browseImage.FileName);
                PictureBox ctrlPictureBox = new PictureBox();
                ctrlPictureBox.Location = new Point(50, 50);
                ctrlPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
                ctrlPictureBox.Name = "picture";
                ctrlPictureBox.Tag = browseImage.FileName;
                ctrlPictureBox.Image = (Image)gambarnya;
                this.pemaksaan();
                ActiveMdiChild.ActiveControl.Controls.Add(ctrlPictureBox);


                //add an event to picture box
                //ctrlPictureBox.MouseDown += new MouseEventHandler(txtLabel_MouseDown);
                //ctrlPictureBox.MouseMove += new MouseEventHandler(ctrl_MouseMove);
                //ctrlPictureBox.MouseUp += new MouseEventHandler(ctrl_MouseUp);

                ctrlPictureBox.MouseDown += new MouseEventHandler(ctrlPictureBox_MouseDown);
                ctrlPictureBox.MouseUp += new MouseEventHandler(ctrlPictureBox_MouseUp);
                ctrlPictureBox.MouseMove += new MouseEventHandler(ctrlPictureBox_MouseMove);

                ////bring to front
         

            }
        }

        void ctrlPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            // Retrieve a reference to the active shape.
            Control currentCtrl;
            currentCtrl = (Control)sender;


            if (isDragging)
            {
                // Move the control.
                currentCtrl.Left = e.X + currentCtrl.Left - clickOffsetX;
                currentCtrl.Top = e.Y + currentCtrl.Top - clickOffsetY;
            }
            else if (isResizing)
            {
                // Resize the control, according to the resize mode.
                if (currentCtrl.Cursor == Cursors.SizeNWSE)
                {
                    currentCtrl.Width = e.X;
                    currentCtrl.Height = e.Y;
                }
                else if (currentCtrl.Cursor == Cursors.SizeNS)
                {
                    currentCtrl.Height = e.Y;
                }
                else if (currentCtrl.Cursor == Cursors.SizeWE)
                {
                    currentCtrl.Width = e.X;
                }
            }
            else
            {
                // Change the cursor if the mouse pointer is on one of the edges
                // of the control.
                if (((e.X + 5) > currentCtrl.Width) &&
                   ((e.Y + 5) > currentCtrl.Height))
                {
                    currentCtrl.Cursor = Cursors.SizeNWSE;
                }
                else if ((e.X + 5) > currentCtrl.Width)
                {
                    currentCtrl.Cursor = Cursors.SizeWE;
                }
                else if ((e.Y + 5) > currentCtrl.Height)
                {
                    currentCtrl.Cursor = Cursors.SizeNS;
                }
                else
                {
                    // This misleadingly named cursor is the four-way
                    // mouse pointer often used for moving objects.
                    currentCtrl.Cursor = Cursors.SizeAll;
                }
            }
        }

        void ctrlPictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            isDragging = false;
            isResizing = false;

            // Invalidate to show the focus rectangle.
            Invalidate();

        }

        void ctrlPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            // Retrieve a reference to the active label.           
            PictureBox currentPicture;
            currentPicture = (PictureBox)sender;

            // Required for the focus rectangle.
            pictureToFocus = currentPicture;

            // Invalidate to show the focus rectangle.
            Invalidate();
            if (e.Button == MouseButtons.Right)
            {
                // Show the context menu.
                //currentLabel.ContextMenuStrip.Show(currentLabel, new Point(e.X, e.Y));
                contextMenuStripPicture.Show(currentPicture, new Point(e.X, e.Y));
                //currentLabel.Text = "babi";
            }

            if (e.Button == MouseButtons.Left)
            {
                clickOffsetX = e.X;
                clickOffsetY = e.Y;

                if (currentPicture.Cursor == Cursors.SizeNWSE ||
                    currentPicture.Cursor == Cursors.SizeNS ||
                    currentPicture.Cursor == Cursors.SizeWE)
                {
                    // The mouse pointer is at one of the sides,
                    // so resizing mode is appropriate.

                    isResizing = true;
                }
                else
                {
                    // The mouse is somewhere else, so dragging mode is
                    // appropriate.
                    isDragging = true;
                }
            }
        }

        private void startButtonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string namaFile = Directory.GetCurrentDirectory().ToString() + "\\NavLibrary\\button_start.gif";
            Bitmap gambar = new Bitmap(namaFile);
            PictureBox ctrlPictureBox = new PictureBox();
            ctrlPictureBox.Name = "StartButton";
            ctrlPictureBox.Tag = namaFile;
            ctrlPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
            ctrlPictureBox.Location = new Point(619, 500);
            ctrlPictureBox.Image = (Image)gambar;
            this.pemaksaan();
            ActiveMdiChild.ActiveControl.Controls.Add(ctrlPictureBox);
            ctrlPictureBox.MouseDown += new MouseEventHandler(ctrlPictureBox_MouseDown);
            ctrlPictureBox.MouseUp += new MouseEventHandler(ctrlPictureBox_MouseUp);
            ctrlPictureBox.MouseMove += new MouseEventHandler(ctrlPictureBox_MouseMove);
        }

        private void nextButtonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string namaFile = Directory.GetCurrentDirectory().ToString() + "\\NavLibrary\\ws_next.gif";
            Bitmap gambar = new Bitmap(namaFile);
            PictureBox ctrlPictureBox = new PictureBox();
            ctrlPictureBox.Name = "NextButton";
            ctrlPictureBox.Tag = namaFile;
            ctrlPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
            ctrlPictureBox.Location = new Point(619, 500);
            ctrlPictureBox.Image = (Image)gambar;
            this.pemaksaan();
            ActiveMdiChild.ActiveControl.Controls.Add(ctrlPictureBox);
            ctrlPictureBox.MouseDown += new MouseEventHandler(ctrlPictureBox_MouseDown);
            ctrlPictureBox.MouseUp += new MouseEventHandler(ctrlPictureBox_MouseUp);
            ctrlPictureBox.MouseMove += new MouseEventHandler(ctrlPictureBox_MouseMove);
        }

        private void previousButtonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string namaFile = Directory.GetCurrentDirectory().ToString() + "\\NavLibrary\\ws_prev.gif";
            Bitmap gambar = new Bitmap(namaFile);
            PictureBox ctrlPictureBox = new PictureBox();
            ctrlPictureBox.Name = "PrevButton";
            ctrlPictureBox.Tag = namaFile;
            ctrlPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
            ctrlPictureBox.Location = new Point(538, 500);
            ctrlPictureBox.Image = (Image)gambar;
            this.pemaksaan();
            ActiveMdiChild.ActiveControl.Controls.Add(ctrlPictureBox);
            ctrlPictureBox.MouseDown += new MouseEventHandler(ctrlPictureBox_MouseDown);
            ctrlPictureBox.MouseUp += new MouseEventHandler(ctrlPictureBox_MouseUp);
            ctrlPictureBox.MouseMove += new MouseEventHandler(ctrlPictureBox_MouseMove);
            
        }

        private void Save()
        {
            
#region fix
            FileInfo fi = new FileInfo(filename);
            DirectoryInfo di = fi.Directory;
            
            XmlTextWriter xtw = new XmlTextWriter(filename, Encoding.UTF8);
            xtw.Formatting = Formatting.Indented;

            //start document
            xtw.WriteStartDocument();
            xtw.WriteComment("SCE Course");
            xtw.WriteStartElement("SCE");
            xtw.WriteElementString("aicc_code", "");
            xtw.WriteElementString("aicc_name", CourseNode.Text);
            xtw.WriteElementString("aicc_description", "");

            //start element modules (level_1)
            xtw.WriteStartElement("modules");

            for (int x = 0; x < CourseNode.Nodes.Count; x++)
            {
                int a = 1 + x;
                //start element module (level_2)
                xtw.WriteStartElement("module");
                xtw.WriteElementString("name", a.ToString());
                xtw.WriteElementString("folder_id", "0");
                xtw.WriteElementString("file", a.ToString() + "\\" + a.ToString() + ".xml");
                xtw.WriteElementString("aicc_code", "");
                xtw.WriteElementString("aicc_name", CourseNode.Nodes[x].Text);
                xtw.WriteElementString("aicc_description", "");
                xtw.WriteElementString("aicc_maxmark", "");
                xtw.WriteElementString("aicc_reqmark", "");
                //end element module (level_2)
                xtw.WriteEndElement();

                if (!Directory.Exists(di.FullName + "\\" + a.ToString()))
                {
                    Directory.CreateDirectory(di.FullName + "\\" + a.ToString());
                    File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\start.hta", di.FullName +  "\\" + a.ToString() + "\\start.hta");
                    File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\start.html", di.FullName + "\\" + a.ToString() + "\\start.html");
                    
                    File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\module.js", di.FullName + "\\" + a.ToString() + "\\module.js");
                    File.Copy(Directory.GetCurrentDirectory().ToString() + "\\courseimages\\course.css", di.FullName + "\\" + a.ToString() + "\\course.css");
                }

                    String ModFile = di.FullName + "\\" + a.ToString() + "\\" + a.ToString() + ".xml";

                    //salahnya disini. xml module harusnya di create di masing-masing directory

                    #region create xml module file
                    XmlTextWriter xtwm = new XmlTextWriter(ModFile, Encoding.UTF8);
                    xtwm.Formatting = Formatting.Indented;
                    //start document
                    xtwm.WriteStartDocument();
                    xtwm.WriteComment("Module file, created by SCE");

                    #region root element module
                    xtwm.WriteStartElement("module");
                    xtwm.WriteAttributeString("format", "0.1");
                    xtwm.WriteAttributeString("rev", "1");
                    xtwm.WriteAttributeString("slideWidth", "750");
                    xtwm.WriteAttributeString("slideHeight", "550");
                    //xtwm.WriteAttributeString("template", "empty");
                    xtwm.WriteAttributeString("file", ModFile);

                    #region level 1 element slides
                    xtwm.WriteStartElement("slides");
                    this.pemaksaan();

                    // bagian ini bisa menimpa file
                    String RuntimeFile = di.FullName + "\\" + a.ToString() + "\\" + "runtime.xml";
                    #region create xml splash file
                    XmlTextWriter xtwr = new XmlTextWriter(RuntimeFile, Encoding.UTF8);
                    xtwr.Formatting = Formatting.Indented;
                    //start document
                    xtwr.WriteStartDocument();
                    xtwr.WriteComment("Runtime file, created by SCE");
                    xtwr.WriteStartElement("runtime");
                    // end of bagian menimpa file

                    for (int y = 0; y < CourseNode.Nodes[x].Nodes.Count; y++)
                    {
                        int b = y + 1;
                        //untuk slide
                        xtwm.WriteStartElement("slide");
                        xtwm.WriteAttributeString("id", b.ToString());
                        xtwm.WriteElementString("name", "SLIDE_" + b.ToString());
                        //object
                        xtwm.WriteStartElement("Objects");
                        //make the splash.xml for the previews
                        if (b == 1)
                        {
                            String SplashFile = di.FullName + "\\" + a.ToString() + "\\" +  "splash.xml";
                            #region create xml splash file
                            XmlTextWriter xtws = new XmlTextWriter(SplashFile, Encoding.UTF8);
                            xtws.Formatting = Formatting.Indented;
                            //start document
                            xtws.WriteStartDocument();
                            xtws.WriteComment("Splash file, created by SCE");
                            xtws.WriteStartElement("splash");
                            xtws.WriteStartElement("settings");
                            xtws.WriteElementString("showsplash", "yes");
                            xtws.WriteEndElement();
                            xtws.WriteStartElement("data");
                            for (int d = 0; d < this.MdiChildren.Length; d++)
                            {
                                SlideDocument tempDocument = (SlideDocument)this.MdiChildren[d];
                                if (tempDocument.TabText == CourseNode.Nodes[x].Text + " -- SLIDE_1")
                                {
                                    tempDocument.Show(dockPanel);
                                    foreach (Control current in tempDocument.ActiveControl.Controls)
                                    {
                                        if (current.Name == "text")
                                        {
                                            xtws.WriteCData("<div style=\"position:absolute;left:" + current.Location.X.ToString() + "px;top:" + current.Location.Y.ToString() + "px;\"><SPAN style=\"FONT-SIZE: 11.5px; FONT-FAMILY: Microsoft Sans Serif\">" + current.Text + "</SPAN></div>");
                                        }
                                        if (current.Name == "picture")
                                        {
                                            FileInfo namafie = new FileInfo(current.Tag.ToString());

                                            if (!Directory.Exists(di.FullName + "\\" + a.ToString() + "\\images"))
                                            {
                                                Directory.CreateDirectory(di.FullName + "\\" + a.ToString() + "\\images");
                                            }
                                            if (!File.Exists(di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name))
                                            {
                                                File.Copy(current.Tag.ToString(), di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                            }
                                            
                                            xtws.WriteCData("<div style=\"position:absolute;left:" + current.Location.X.ToString() + "px;top:" + current.Location.Y.ToString() + "px;\">" + "<img border=\"0\" src=\"images/" + namafie.Name + "\"</img>" + "</div>");

                                        }
                                        if (current.Name == "StartButton")
                                        {
                                            xtws.WriteCData("<div style=\"position:absolute;left:" + current.Location.X.ToString() + "px;top:" + current.Location.Y.ToString() + "px;\"><img border=\"0\" style=\"cursor:hand;cursor:pointer;\" src=\"images/button_start.gif\" onclick=\"StartMyModule();\" /></div>");
                                        }
                                    }
                                    this.current_document();
                                }
                            }
                            
                            xtws.WriteEndElement();
                            xtws.WriteEndElement();
                            xtws.WriteEndDocument();
                            xtws.Close();
                            #endregion create xml splash file
                        }
                        //make the runtime.xml for the previews
                        if (b > 1)
                        {
                           // MessageBox.Show(b.ToString());
                            int next_slide = b + 1;
                            int prev_slide = b - 1;
                            
                            xtwr.WriteStartElement("slide");
                            xtwr.WriteElementString("name", "SLIDE_" + b.ToString());
                            xtwr.WriteStartElement("data");
                            for (int d = 0; d < this.MdiChildren.Length; d++)
                            {
                                SlideDocument tempDocument = (SlideDocument)this.MdiChildren[d];
                                if (tempDocument.TabText == CourseNode.Nodes[x].Text + " -- SLIDE_" + b.ToString())
                                {
                                    tempDocument.Show(dockPanel);
                                    foreach (Control current in tempDocument.ActiveControl.Controls)
                                    {
                                        if (current.Name == "text")
                                        {
                                            xtwr.WriteCData("<div style=\"position:absolute;left:" + current.Location.X.ToString() + "px;top:" + current.Location.Y.ToString() + "px;\"><SPAN style=\"FONT-SIZE: 11.5px; FONT-FAMILY: Microsoft Sans Serif\">" + current.Text + "</SPAN></div>");
                                        }
                                        if (current.Name == "picture")
                                        {
                                            FileInfo namafie = new FileInfo(current.Tag.ToString());

                                            if (!Directory.Exists(di.FullName + "\\" + a.ToString() + "\\images"))
                                            {
                                                Directory.CreateDirectory(di.FullName + "\\" + a.ToString() + "\\images");
                                            }
                                            if (!File.Exists(di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name))
                                            {
                                                File.Copy(current.Tag.ToString(), di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                            }

                                            xtwr.WriteCData("<div style=\"position:absolute;left:" + current.Location.X.ToString() + "px;top:" + current.Location.Y.ToString() + "px;\">" + "<img border=\"0\" src=\"images/" + namafie.Name + "\"</img>" + "</div>");

                                        }
                                        if (current.Name == "NextButton")
                                        {
                                            xtwr.WriteCData("<div style=\"position:absolute;left:" + current.Location.X.ToString() + "px;top:" + current.Location.Y.ToString() + "px;\"><img border=\"0\" style=\"cursor:hand;cursor:pointer;\" src=\"images/ws_next.gif\" onclick=\"NextButton('SLIDE_"+ next_slide.ToString() +"');\" /></div>");
                                        }
                                        if (current.Name == "PrevButton")
                                        {
                                            xtwr.WriteCData("<div style=\"position:absolute;left:" + current.Location.X.ToString() + "px;top:" + current.Location.Y.ToString() + "px;\"><img border=\"0\" style=\"cursor:hand;cursor:pointer;\" src=\"images/ws_prev.gif\" onclick=\"NextButton('SLIDE_" + prev_slide.ToString() + "');\" /></div>");
                                        }
                                    }
                                    this.current_document();
                                }
                            }
                            xtwr.WriteEndElement();
                            xtwr.WriteEndElement();
                           
                            #endregion create xml runtime file
                        }
                        //looping for object in every slide
                        for (int d = 0; d < this.MdiChildren.Length; d++)
                        {
                            SlideDocument tempDocument = (SlideDocument)this.MdiChildren[d];
                            if (tempDocument.TabText == CourseNode.Nodes[x].Text + " -- SLIDE_" + b.ToString())
                            {
                                tempDocument.Show(dockPanel);
                                foreach (Control current in tempDocument.ActiveControl.Controls)
                                {
                                    xtwm.WriteStartElement("object");
                                    if (current.Name == "text")
                                    {
                                        xtwm.WriteElementString("SLIDE_ID", "SLIDE_" + b.ToString()); 
                                        xtwm.WriteElementString("type", "text");
                                        xtwm.WriteElementString("value", current.Text);
                                        xtwm.WriteElementString("X", current.Location.X.ToString());
                                        xtwm.WriteElementString("Y", current.Location.Y.ToString());
                                        
                                    }
                                    if (current.Name == "picture")
                                    {
                                        xtwm.WriteElementString("SLIDE_ID", "SLIDE_" + b.ToString());
                                        xtwm.WriteElementString("type", "picture");

                                        FileInfo namafie = new FileInfo(current.Tag.ToString());

                                        if (!Directory.Exists(di.FullName + "\\" + a.ToString() + "\\images"))
                                        {
                                            Directory.CreateDirectory(di.FullName + "\\" + a.ToString() + "\\images");
                                        }
                                        if (!File.Exists(di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name))
                                        {
                                            File.Copy(current.Tag.ToString(), di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                        }
                                        xtwm.WriteElementString("location", di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                        xtwm.WriteElementString("X", current.Location.X.ToString());
                                        xtwm.WriteElementString("Y", current.Location.Y.ToString());

                                    }
                                    if (current.Name == "StartButton")
                                    {
                                        xtwm.WriteElementString("SLIDE_ID", "SLIDE_" + b.ToString());
                                        xtwm.WriteElementString("type", "StartButton");

                                        FileInfo namafie = new FileInfo(current.Tag.ToString());

                                        if (!Directory.Exists(di.FullName + "\\" + a.ToString() + "\\images"))
                                        {
                                            Directory.CreateDirectory(di.FullName + "\\" + a.ToString() + "\\images");
                                        }
                                        if (!File.Exists(di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name))
                                        {
                                            File.Copy(current.Tag.ToString(), di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                        }
                                        xtwm.WriteElementString("location", di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                        xtwm.WriteElementString("X", current.Location.X.ToString());
                                        xtwm.WriteElementString("Y", current.Location.Y.ToString());

                                    }
                                    if (current.Name == "NextButton")
                                    {
                                        xtwm.WriteElementString("SLIDE_ID", "SLIDE_" + b.ToString());
                                        xtwm.WriteElementString("type", "NextButton");

                                        FileInfo namafie = new FileInfo(current.Tag.ToString());

                                        if (!Directory.Exists(di.FullName + "\\" + a.ToString() + "\\images"))
                                        {
                                            Directory.CreateDirectory(di.FullName + "\\" + a.ToString() + "\\images");
                                        }
                                        if (!File.Exists(di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name))
                                        {
                                            File.Copy(current.Tag.ToString(), di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                        }
                                        xtwm.WriteElementString("location", di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                        xtwm.WriteElementString("X", current.Location.X.ToString());
                                        xtwm.WriteElementString("Y", current.Location.Y.ToString());

                                    }
                                    if (current.Name == "PrevButton")
                                    {
                                        xtwm.WriteElementString("SLIDE_ID", "SLIDE_" + b.ToString());
                                        xtwm.WriteElementString("type", "PrevButton");

                                        FileInfo namafie = new FileInfo(current.Tag.ToString());

                                        if (!Directory.Exists(di.FullName + "\\" + a.ToString() + "\\images"))
                                        {
                                            Directory.CreateDirectory(di.FullName + "\\" + a.ToString() + "\\images");
                                        }
                                        if (!File.Exists(di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name))
                                        {
                                            File.Copy(current.Tag.ToString(), di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                        }
                                        xtwm.WriteElementString("location", di.FullName + "\\" + a.ToString() + "\\images\\" + namafie.Name);
                                        xtwm.WriteElementString("X", current.Location.X.ToString());
                                        xtwm.WriteElementString("Y", current.Location.Y.ToString());

                                    }
                                    xtwm.WriteEndElement();

                                }
                                this.current_document();
                            }
                        }
                        //objetcs
                        xtwm.WriteEndElement();
                        //end slide
                        xtwm.WriteEndElement();
                    }


                //tandanya
                    xtwr.WriteEndElement();
                    xtwr.WriteEndDocument();
                    xtwr.Close();
                //tandanya



                    xtwm.WriteEndElement();
                    #endregion

                    xtwm.WriteEndDocument();
                    xtwm.Close();
                    #endregion
                    #endregion create xml module file
            }
                        
                //end element modules (level_1)
                xtw.WriteEndElement();

                //windows
                xtw.WriteStartElement("windows");

                for (int m = 0; m < CourseNode.Nodes.Count; m++)
                {
                    for (int y = 0; y < CourseNode.Nodes[m].Nodes.Count; y++)
                    {
                        int f = m + 1;
                        int b = y + 1;
                        //MessageBox.Show("Course :" + CourseNode.Nodes[x].Text + " -- Slide :" + CourseNode.Nodes[x].Nodes[y].Text);
                        //window
                        xtw.WriteStartElement("window");
                        xtw.WriteElementString("module", f.ToString());
                        xtw.WriteElementString("zoom", "100");
                        xtw.WriteElementString("active", "true");
                        xtw.WriteElementString("slide", "SLIDE_" + b.ToString());
                        //window
                        xtw.WriteEndElement();
                    }
                }
           


            //windows
            xtw.WriteEndElement();

            //root
            xtw.WriteEndElement();
            xtw.WriteEndDocument();
            xtw.Close();
#endregion
        }

        private void pemaksaan()
        {
            for (int x = 0; x < this.MdiChildren.Length; x++)
            {
                SlideDocument tempDocument = (SlideDocument)this.MdiChildren[x];

                if (tempDocument == ActiveMdiChild)
                {
                    tempDocument.Show(dockPanel);
                    windowname = tempDocument.TabText;
                }
            }
        }

        private void current_document()
        {
            for (int x = 0; x < this.MdiChildren.Length; x++)
            {
                SlideDocument tempDocument = (SlideDocument)this.MdiChildren[x];

                if (tempDocument.TabText == windowname)
                {
                    tempDocument.Show(dockPanel);
                    
                }
            }
        }

        private void removeToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Control currentPicture = contextMenuStripPicture.SourceControl;
            ActiveMdiChild.ActiveControl.Controls.Remove(currentPicture);
        }

        private void ToolStripLabel1_Click(object sender, EventArgs e)
        {
           
        }

        private void ModuleToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void SaveToolStripButton_Click(object sender, EventArgs e)
        {
            this.Save();
        }

        private void previewModuleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int indexnode = PanelCourse.treeView1.SelectedNode.Index;
            int moduleDirectory = indexnode + 1;
            FileInfo fi = new FileInfo(filename);
            DirectoryInfo di = fi.Directory;
            
            this.Save();
            System.Diagnostics.Process.Start(@di.FullName.ToString() + "\\" + moduleDirectory.ToString() + "\\start.hta");
        }

        private void PublishToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Publish publish = new Publish();
            FileInfo fi = new FileInfo(filename);
            DirectoryInfo di = fi.Directory;

            Guid myGuid = Guid.NewGuid();
            if (publish.ShowDialog() == DialogResult.OK)
            {
                this.Save();
                XmlTextWriter xtw = new XmlTextWriter(di.ToString() + "\\imsmanifest.xml", Encoding.Unicode);
                xtw.Formatting = Formatting.Indented;

                //start document
                xtw.WriteStartDocument();
                xtw.WriteComment("ims manifest");
                xtw.WriteStartElement("manifest");
                xtw.WriteAttributeString("xmlns", "http://www.imsglobal.org/xsd/imscp_v1p1");
                xtw.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
                xtw.WriteAttributeString("xmlns:adlcp", "http://www.adlnet.org/xsd/adlcp_v1p3");
                xtw.WriteAttributeString("xmlns:imsss", "http://www.imsglobal.org/xsd/imsss");
                xtw.WriteAttributeString("xmlns:adlseq", "http://www.adlnet.org/xsd/adlseq_v1p3");
                xtw.WriteAttributeString("xmlns:adlnav", "http://www.adlnet.org/xsd/adlnav_v1p3");
                xtw.WriteAttributeString("identifier", "MANIFEST-" + myGuid.ToString());
                xtw.WriteAttributeString("xsi:schemaLocation", "http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.adlnet.org/xsd/adlcp_v1p3 adlcp_v1p3.xsd http://www.imsglobal.org/xsd/imsss imsss_v1p0.xsd http://www.adlnet.org/xsd/adlseq_v1p3 adlseq_v1p3.xsd http://www.adlnet.org/xsd/adlnav_v1p3 adlnav_v1p3.xsd");
                xtw.WriteAttributeString("version", "1.0");

                xtw.WriteStartElement("metadata");
                xtw.WriteElementString("schema", "ADL SCORM");
                xtw.WriteElementString("schemaversion", "CAM 1.3");
                xtw.WriteEndElement();
                xtw.WriteStartElement("organizations");
                xtw.WriteAttributeString("default", publish.txtCourseId.Text);

                xtw.WriteStartElement("organization");
                xtw.WriteAttributeString("identifier", publish.txtCourseId.Text);
                xtw.WriteElementString("title", CourseNode.Text);

                for (int x = 0; x < CourseNode.Nodes.Count; x++)
                {
                    int a = x + 1;
                    xtw.WriteStartElement("item");
                    xtw.WriteAttributeString("identifier", CourseNode.Nodes[x].Text);
                    xtw.WriteAttributeString("identifierref", "RES" + a.ToString());
                    xtw.WriteAttributeString("parameters", "?width=750&height=550");
                    xtw.WriteElementString("title", CourseNode.Nodes[x].Text);
                    xtw.WriteEndElement();
                }


                xtw.WriteEndElement();

                xtw.WriteEndElement();

                xtw.WriteStartElement("resources");
                xtw.WriteStartElement("resource");
                xtw.WriteAttributeString("identifier", "RESCOURSEIMAGES");
                xtw.WriteAttributeString("adlcp:scormType", "asset");
                xtw.WriteAttributeString("type", "webcontent");
                //xtw.WriteStartElement("file");
                //xtw.WriteAttributeString("href", "courseimages/agenthub.swf");
                //xtw.WriteEndElement();
                xtw.WriteStartElement("file");
                xtw.WriteAttributeString("href", "courseimages/lab.js");
                xtw.WriteEndElement();
                xtw.WriteStartElement("file");
                xtw.WriteAttributeString("href", "courseimages/legacy.js");
                xtw.WriteEndElement();
                xtw.WriteStartElement("file");
                xtw.WriteAttributeString("href", "courseimages/lms.js");
                xtw.WriteEndElement();

                xtw.WriteEndElement();


                //resource 2
                for (int x = 0; x < CourseNode.Nodes.Count; x++)
                {
                    int a = x + 1;
                    xtw.WriteStartElement("resource");
                    xtw.WriteAttributeString("identifier", "RES" + a.ToString());
                    xtw.WriteAttributeString("adlcp:scormType", "sco");
                    xtw.WriteAttributeString("href", a.ToString() + "/start.html");
                    xtw.WriteAttributeString("type", "webcontent");
                    xtw.WriteStartElement("file");
                    xtw.WriteAttributeString("href", a.ToString() + "/module.js");
                    xtw.WriteEndElement();
                    xtw.WriteStartElement("file");
                    xtw.WriteAttributeString("href", a.ToString() + "/start.html");
                    xtw.WriteEndElement();
                    xtw.WriteStartElement("file");
                    xtw.WriteAttributeString("href", a.ToString() + "/splash.xml");
                    xtw.WriteEndElement();
                    xtw.WriteStartElement("file");
                    xtw.WriteAttributeString("href", a.ToString() + "/runtime.xml");
                    xtw.WriteEndElement();
                    xtw.WriteStartElement("file");
                    xtw.WriteAttributeString("href", a.ToString() + "/course.css");
                    xtw.WriteEndElement();
                    if (Directory.Exists(di.FullName + "\\" + a.ToString() + "\\images"))
                    {
                        FileInfo[] rgetfile = di.GetFiles(a.ToString() + "/images/*");
                        foreach (FileInfo re in rgetfile)
                        {
                            xtw.WriteStartElement("file");
                            xtw.WriteAttributeString("href", a.ToString() + "/images/" + re.Name);
                            xtw.WriteEndElement();
                        }
                    }

                    xtw.WriteEndElement();



                }

                xtw.WriteEndElement();




                xtw.WriteEndElement();
                
                xtw.WriteEndDocument();
                xtw.Close();
                
                //Directory.CreateDirectory(di.ToString() + "\\temp");

                //copy file to the temp directory
                copyDirectory( Directory.GetCurrentDirectory() + "\\Scorm" , di.ToString() + "\\temp");
                copyDirectory(di.ToString() + "\\courseimages", di.ToString() + "\\temp\\courseimages");
                File.Copy(di.ToString() + "\\imsmanifest.xml", di.ToString() + "\\temp\\imsmanifest.xml", true);
                for (int x = 0; x < CourseNode.Nodes.Count; x++)
                {
                    int a = x + 1;
                    Directory.CreateDirectory(di.ToString() + "\\temp\\" + a.ToString());
                    File.Copy(di.ToString() + "\\" + a.ToString() + "\\start.html", di.ToString() + "\\temp\\" + a.ToString() + "\\start.html", true);
                    File.Copy(di.ToString() + "\\" + a.ToString() + "\\splash.xml", di.ToString() + "\\temp\\" + a.ToString() + "\\splash.xml", true);
                    File.Copy(di.ToString() + "\\" + a.ToString() + "\\runtime.xml", di.ToString() + "\\temp\\" + a.ToString() + "\\runtime.xml", true);
                    File.Copy(di.ToString() + "\\" + a.ToString() + "\\course.css", di.ToString() + "\\temp\\" + a.ToString() + "\\course.css", true);
                    File.Copy(di.ToString() + "\\" + a.ToString() + "\\module.js", di.ToString() + "\\temp\\" + a.ToString() + "\\module.js", true);

                    if(Directory.Exists(di.ToString() + "\\" + a.ToString() + "\\images"))
                    {
                        copyDirectory(di.ToString() + "\\" + a.ToString() + "\\images", di.ToString() + "\\temp\\" + a.ToString() + "\\images");
                    }
                }
            
                

                //make zip file
                string filezip = publish.txtLocation.Text + "\\" + publish.txtFileName.Text + ".zip"; 

                ICSharpCode.SharpZipLib.Zip.FastZip z = new ICSharpCode.SharpZipLib.Zip.FastZip();
                z.CreateEmptyDirectories = true;

                z.CreateZip(filezip, di.ToString() + "\\temp", true, "");


                //delete temp directory
                Directory.Delete(di.ToString() + "\\temp", true);
            }

        }
        public static void copyDirectory(string Src, string Dst)
        {
            String[] Files;

            if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
                Dst += Path.DirectorySeparatorChar;
            if (!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
            Files = Directory.GetFileSystemEntries(Src);
            foreach (string Element in Files)
            {
                // Sub directories
                if (Directory.Exists(Element))
                    copyDirectory(Element, Dst + Path.GetFileName(Element));
                // Files in directory
                else
                    File.Copy(Element, Dst + Path.GetFileName(Element), true);
            }
        }

        private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Save();
        }

    }
}
